<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <meta http-equiv='X-UA-Compatible' content='ie=edge'>
    <title>Document</title>
</head>
<body>
    <!-- 挂载点 -->
    <div id="app">
        <book v-for="item in list" 
        :key="item.id"
        :name="item.name"
        :price="item.price"
        :num="item.num"
        @my-add="addNum(item.id,$event)"
        @my-redu="reduNum(item.id,$event)">

        </book>
    </div>
</body>
</html>
<script src="js/vue.js"></script>
<script>
    Vue.component('book',{
        props:['name','price','num'],

        template:`
            <div>
                书名：{{ name }} 
                价格：{{ price }} 
                数量：<button @click="redu">-</button>{{ num }} <button @click="add">+</button>
                总价 {{ num*price }}
                
            </div>
        `,

        methods: {
            add(){
                this.$emit('my-add',1);
            },

            redu(){
                this.$emit('my-redu',1);
            }
        }
    })
    const app = new Vue({
        el: '#app',

        data: {
            list: [
                {id:001,name:'西游记',price:100,num:1},
                {id:002,name:'红楼梦',price:200,num:1},
                {id:003,name:'水浒传',price:200,num:1}
            ]
        },

        methods: {
            addNum(id,pnum){
                this.list.find(item=>item.id===id).num += pnum;
            },

            reduNum(id,pnum){
                this.list.find(item=>item.id===id).num -= pnum;
            }

        }
    })
</script>